<!DOCTYPE html>
<html lang="zh-cn">
  <head>
    <meta charset="utf-8" />
    <meta
      name="viewport"
      content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no"
    />
    <meta http-equiv="X-UA-Compatible" content="IE=edge,chrome=1" />
    <title>26. 删除有序数组中的重复项</title>
  </head>
  <body>
    <!-- start -->
    <div>https://leetcode.cn/problems/remove-duplicates-from-sorted-array/</div>
    <!-- end -->
  </body>
  <!-- js start -->
  <script type="text/javascript">
    /**
     * @param {number[]} nums
     * @return {number}
     */
    var removeDuplicates = function (nums = []) {
      let cache = {};

      for (let i = 0; i < nums.length; i++) {
        if (!cache[nums[i]]) {
          cache[nums[i]] = 1;
          continue;
        }

        if (cache[nums[i]] === 1) {
          //   i--;
          nums[i] = '_';
          nums.splice(i, 1);
          i--;
        }
      }

    //   console.log(cache, nums);

      return Object.keys(cache).length;
    };

    /*******************************************************************************
     * 测试
     ******************************************************************************/
    // console.log(removeDuplicates([1, 1, 2]));
    console.log(removeDuplicates([0, 0, 1, 1, 1, 2, 2, 3, 3, 4]));
  </script>
</html>
